package cn.wolfcode.luowowo.acticle.service.impl;


import cn.wolfcode.luowowo.acticle.domain.StrategyCommend;
import cn.wolfcode.luowowo.acticle.domain.StrategyDetail;
import cn.wolfcode.luowowo.acticle.mapper.StrategyCommendMapper;
import cn.wolfcode.luowowo.acticle.mapper.StrategyDetailMapper;
import cn.wolfcode.luowowo.acticle.service.IStrategyCommendService;
import cn.wolfcode.luowowo.common.query.QueryObject;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Component
@Service
@Transactional
public class StrategyCommendServiceImpl implements IStrategyCommendService {

    @Autowired
    private StrategyCommendMapper strategyCommendMapper;
    @Autowired
    private StrategyDetailMapper strategyDetailMapper;

    @Override
    public PageInfo query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        return new PageInfo(strategyCommendMapper.selectForList(qo));
    }

    @Override
    public void saveOrUpdate(StrategyCommend strategyCommend) {

        //目的地
        StrategyDetail detail = strategyDetailMapper.selectByPrimaryKey(strategyCommend.getDetail().getId());

        if(strategyCommend.getId()  == null){
            strategyCommendMapper.insert(strategyCommend);
        }else{
            strategyCommendMapper.updateByPrimaryKey(strategyCommend);
        }
    }

    @Override
    public void delete(Long id) {
        strategyCommendMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<StrategyCommend> getHotStrategyCommendTopN(int num) {
        PageHelper.startPage(1, num, " sc.sequence ");
        return strategyCommendMapper.selectHostStrategyCommendTopN();
    }
}
